<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList"
        :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
          >
            <el-table-column prop="name" label="路径名称" min-width="200"/>
            <el-table-column prop="domain" label="专业领域" width="120">
              <template slot-scope="scope">
                {{ getDomainName(scope.row.domain) }}
              </template>
            </el-table-column>
            <el-table-column prop="description" label="路径描述" min-width="200"/>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
                  {{ scope.row.status === 1 ? '启用' : '禁用' }}
                </el-tag>
              </template>
            </el-table-column>

            <el-table-column label="操作" fixed="right" width="250">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onManageNodes(scope.row)">管理节点</el-button>
                <el-button type="text" @click="onToggleStatus(scope.row)">
                  {{ scope.row.status === 1 ? '禁用' : '启用' }}
                </el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "LearningPathIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.resourcePath,
      searchList: [
        {
          label: '路径名称',
          prop: 'name',
          name: 'input',
          clearable: true
        },
        {
          label: '专业领域',
          prop: 'domain',
          name: 'select',
          clearable: true,
          options: [
            { label: '铁道工程', value: 1 },
            { label: '信号控制', value: 2 },
            { label: '机车车辆', value: 3 }
          ]
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '启用', value: 1 },
            { label: '禁用', value: 0 }
          ]
        }
      ],
      searchParams: {
        name: '',
        domain: '',
        status: ''
      },
      defaultParams: {},
      tableData: []
    }
  },
  methods: {
    getDomainName(domain) {
      const domains = {
        1: '铁道工程',
        2: '信号控制',
        3: '机车车辆'
      }
      return domains[domain] || '-'
    },
    onAdd() {
      this.$refs.addFormRef.open()
    },
    onEdit(row) {
      this.$refs.addFormRef.open(row)
    },
    onManageNodes(row) {
      this.$router.push({
        name: 'PathNodeRelation',
        params: {
          pathId: row.id
        }
      })
    },
    onToggleStatus(row) {
      const action = row.status === 1 ? '禁用' : '启用'
      this.$confirm(`确认${action}该学习路径吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        http.resourcePath.update({id: row.id,  status: row.status === 1 ? 0 : 1 }).then(() => {
          this.$message.success(`${action}成功`)
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    onDelete(row) {
      this.$confirm('确认删除该学习路径吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        http.resourcePath.delete(row.id).then(() => {
          this.$message.success('删除成功')
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
